html='''
<div class="wrap">
<div id="container">
<ul class="list">
<li class = "item-0">first item</li>
<li class = "item-1"><a href = "link2.html">second item</a></li>
<li class = "item-0 active"><a href = "link3.html"><span class="bold">third item</span></a></li>
<li class = "item-1" active><a href = "link4.html">fourth item</a></li>
<li class = "item-0"><a href = "link5.html">fif item</a>
</ul>
</div>
</div>
'''
from pyquery import PyQuery as pq
doc = pq(html)
items = doc('.list')
parents = items.parents()#祖先节点
print(type(parents))
print(parents)
# 正确答案
# <div class="wrap">
# <div id="container">
# <ul class="list">
# <li class="item-0">first item</li>
# <li class="item-1"><a href="link2.html">second item</a></li>
# <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>
# <li class="item-0"><a href="link5.html">fif item</a>
# </li></ul>
# </div>
# </div>
# <div id="container">
# <ul class="list">
# <li class="item-0">first item</li>
# <li class="item-1"><a href="link2.html">second item</a></li>
# <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>
# <li class="item-0"><a href="link5.html">fif item</a>
# </li></ul>
# </div>
print('*******************')
parent = items.parents('.wrap')
print(parent)
# 正确答案
# <div class="wrap">
# <div id="container">
# <ul class="list">
# <li class="item-0">first item</li>
# <li class="item-1"><a href="link2.html">second item</a></li>
# <li class="item-0 active"><a href="link3.html"><span class="bold">third item</span></a></li>
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>
# <li class="item-0"><a href="link5.html">fif item</a>
# </li></ul>
# </div>
# </div>

# 兄弟节点
li = doc('.html .item-0.active')#先选择class为list的节点内部class为item-0和active的节点，也就是第三个li节点，他有4个兄弟节点
print(li.siblings())
# 输出
# <li class="item-0">first item</li>
# <li class="item-1"><a href="link2.html">second item</a></li>
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>
# <li class="item-0"><a href="link5.html">fif item</a>

li = doc('.list .item-0.active')
print(li.siblings('.active'))
# 输出
# <li class="item-1" active=""><a href="link4.html">fourth item</a></li>

# 遍历
li = doc('.item-0.active')
print(li)
print(str(li))#对单个节点来说可以直接打印输出，也可以直接转换成字符串

lis = doc('li').items()#调用后，得到一个生成器，遍历一下，就可以得到li节点对象了
print(type(lis))
for li in lis:
    print(li,type(li))

# 获取信息
# 获取属性

a = doc('.item-0.active a')
print(a,type(a))
print(a.attr('href'))
print(a.attr.href)

a = doc('a')
print(a,type(a))
# 当返回结果包含多个节点时，调用attr（）方法，只会得到第一个节点的属性
print(a.attr('href'))
print(a.attr.href)
# 遍历获取所有属性

for item in a.items():
    print(item.attr('href'))
# 获取文本
a = doc('.item-0.active a')
print(a)
print(a.text())

li = doc('.item-0.active')
print(li)
print(li.html())#选中了第三个li节点，返回li节点内的所有HTML文本
print('****************')


html='''
<div class=wrap">
<div id="container">
<ul class="list">
<li class = "item-1"><a href = "link2.html">second item</a></li>
<li class = "item-0 active"><a href = "link3.html"><span class="bold">third item</span></a></li>
<li class = "item-1" active><a href = "link4.html">fourth item</a></li>
<li class = "item-0"><a href = "link5.html">fif item</a>
</ul>
</div>
</div>
'''
doc = pq(html)
li = doc('li')
print(li.html())#返回的是li节点内部的HTML文本
# 如果想要获取每个节点的内部HTML文本，则需要遍历每一个节点，而，text（）方法不需要遍历就可以获取，它将所有节点取文本之后合并成一个字符串
print(li.text())#纯文本，中间用一个空格分隔开，表示返回结果是一个字符串

print(type(li.html()))
print(type(li.text()))